package org.feifei.hrm.service.impl;

import org.feifei.hrm.client.LoginUserClient;
import org.feifei.hrm.domain.Employee;
import org.feifei.hrm.domain.Tenant;
import org.feifei.hrm.mapper.EmployeeMapper;
import org.feifei.hrm.service.IEmployeeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.feifei.hrm.vo.UserContextInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feifei
 * @since 2022-01-02
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private LoginUserClient loginUserClient;
    @Autowired
    private EmployeeMapper employeeMapper;
    //做增删改员工同步修改loginUser  @TODO
    @Override
    @Transactional
    public boolean insert(Employee entity) {
        return super.insert(entity);
    }

    @Override
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }

    @Override
    public boolean updateById(Employee entity) {
        return super.updateById(entity);
    }

    @Override
    public UserContextInfo getEmpAndTenantByLoginId(Long loginId) {

        //查看员工及租户信息
        Employee  employee = employeeMapper
                .loadEmpAndTenantByLoginId(loginId);
        //封装为要返回UserContextInfo
        UserContextInfo result = new UserContextInfo();
        result.setEmployeeId(employee.getId());
        result.setEmpUsername(employee.getUsername());
        Tenant tenant = employee.getTenant();
        if (tenant != null) {
            result.setTenantId(tenant.getId());
            result.setTenantName(tenant.getCompanyName());
        }
        return result;
    }
}